package com.powermgr.boringtest.service.impl;

import com.alibaba.fastjson.JSON;
import com.powermgr.boringtest.bean.VisitData;
import com.powermgr.boringtest.result.Result;
import com.powermgr.boringtest.service.IVisitService;
import com.powermgr.boringtest.utils.ReadWriteUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.io.IOException;

import static com.powermgr.boringtest.utils.ConfigUtils.VISIT_JSON;

/**
 * AccessServiceImpl
 *
 * @author qiuxiangdong
 * @date 2023/6/12
 */
@Log4j2
@Service
public class VisitServiceImpl implements IVisitService {
    private VisitData visitData;
    @PostConstruct
    private void initAccess(){
        log.info("初始化 VisitServiceImpl.java ");
        try {
            String accessStr = ReadWriteUtils.readFile(VISIT_JSON);
            visitData = JSON.parseObject(accessStr, VisitData.class);
            log.info("获取 visit.json ");
        } catch (IOException e) {
            log.error("获取 visit.json 失败", e);
        }
    }
    @Override
    public void incrementVisit() {
        visitData.getVisited().incrementAndGet();
    }
    @Override
    public void incrementAnalyze() {
        visitData.getAnalyzed().incrementAndGet();
    }
    @Override
    public VisitData getVisitData() {
        return visitData;
    }
    @Override
    public Result<Object> getCurrentStatistics() {
        return Result.success("success",visitData);
    }
}
